/*
Author - CG DEV
Class Name - ExpressRegistrationController 
Date Created - 21/10/2011
Purpose - Controller for ExpressRegistrationPage
*/

public class ExpressRegistrationExtension {
    private final Lead weblead;  
    public Id projectId {get; set;}
    public id rectypeId;
    public string leadFirstName{get; set;}  
    public string leadLastName{get; set;}  
    public string leadEmail{get; set;}
    public string leadMobile{get; set;}
    public string leadstdCode{get;set;}
    public string leadPhone{get; set;}
    public String city{get;set;}

    public string CompanyName{get;set;}
    public string ProjectType{get;set;}
    string recType;
    public String AccommodationValue{get;set;}
    public String BudgetInLakhs{get;set;}
    List<String> accTypeList = new List<String>();
    public String DateOfVisit{get;set;}
    public String Purpose{get;set;}
    public String PlotNo{get;set;}
    public String Street{get;set;}
    public string Locality{get;set;}
    public string PinCode{get;set;}
    public String Occupation{get;set;}
    public boolean bdisableButton{get;set;}
    public String CampaignName{get;set;}
    public string publisher{get;set;}

    public String[] accomTypes {get;set;}
    public boolean IsValidEmail {get;set;}
    public boolean IsValidPlotNo {get;set;}
    public boolean IsValidPinCode {get;set;}
    public boolean IsValidMobile {get;set;}
    public String Country{get;set;}    
    public  String others {get;set;}
    
    public ExpressRegistrationExtension(ApexPages.StandardController stdController) {
       try
       {
       Country = 'India';
       weblead = (Lead)stdController.getRecord();  
       projectId = ApexPages.currentPage().getParameters().get('projectid');
       CampaignName= ApexPages.currentPage().getParameters().get('campaign');
       publisher= ApexPages.currentPage().getParameters().get('publisher');

       if(projectId==null)
       {
           bdisableButton=true;
           //ApexPages.addMessages('Project Id can not be blank'); 
           
       }
       else
       {
           bdisableButton=false;
       }

       accomTypes = new String[] {};

       if(projectId != null)
       { 
           system.debug('Project Id :' + projectId);
           List<Project_Name__c> projectList = [Select Record_Type__c,Project_Type__c,Type_of_Accomodation__c from Project_Name__c where id = :projectId];

           system.debug('Project Id :' + projectList[0].Record_Type__c);
           if(projectList.size()>0)
           {
               Project_Name__c objProjectName=projectList[0];
               recType=objProjectName.Record_Type__c;
               if(objProjectName.Project_Type__c!=null && objProjectName.Project_Type__c!='')
               {
                   ProjectType=objProjectName.Project_Type__c.touppercase();
                   
               }
               
               String accType=objProjectName.Type_of_Accomodation__c;
               rectypeId = RecordTypeHelper.getRecordTypeId('Lead',recType);
               
               if(accType!=null && accType!='')
               {
                   accTypeList=accType.split(';');
                   
               }
           }
       }

       }
       catch(Exception exp)
       {
           String CaseException=exp.getMessage() + '---:--- Line Number is--:' + String.valueOf(exp.getLineNumber());
           SendErrorMail(CaseException);
       }
    }
    
    private boolean ShowDesignationCompany(String projectType)
    {
        return true;
    }
    private void SendErrorMail(String exception1)
    {
        try
        {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String EmailID1='shradha.jaiswal@lodhagroup.com';
            String EmailID2='mangesh.patil@in.fujitsu.com';
            String[] toAdd =  new string[]{EmailID1,EmailID2};
            mail.setToAddresses(toAdd);
            mail.setReplyTo('noreply@lodhagroup.com');
            mail.setSenderDisplayName('Lodha Group');
            mail.setSubject('Customer creation error');
            mail.sethtmlbody(exception1);
            mail.setBccSender(false);
            mail.setUseSignature(false);
            List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.Email[]{ mail });
        }
        catch(Exception exp)
        {
            
        }
        
    }
    public PageReference saveLead() {  
    try {
    
        weblead.Project_Name__c = projectId;
        weblead.LeadSource = 'Express Registration';
        weblead.FirstName = leadFirstName;
        weblead.LastName = leadLastName;
        
      
        if(DateOfVisit.length() != 0)
        {
            Date dt = Date.parse(DateOfVisit);  
            weblead.Date_Of_Visit__c =dt;
        }
        weblead.Email = leadEmail;

        if(city != null && city != '')
        {
            if(ProjectType!='COMMERCIAL')
                weblead.City_R__c = city;
            else
                weblead.City_O__c=city;
        }
        else
        {
            if(ProjectType!='COMMERCIAL')
                weblead.City_R__c = city;
            else
                weblead.City_O__c=city;
        }
        weblead.Mobile__c = leadMobile;
        weblead.STD_Code__c = leadstdCode;
        weblead.Phone = leadPhone; 
        weblead.Recordtypeid = rectypeId;
        weblead.country_O__c = Country;
        
        
        if(BudgetInLakhs == '20-30 lacs')
        {
            weblead.Budget_in_Lakhs__c = 30;
        }
        else if(BudgetInLakhs == '31-40 lacs')
        {
            weblead.Budget_in_Lakhs__c = 40;
        }
        else if(BudgetInLakhs == '41-50 lacs')
        {
            weblead.Budget_in_Lakhs__c = 50;
        }
        else if(BudgetInLakhs == '51 and Above')
        {
            weblead.Budget_in_Lakhs__c = 51;
        }
        weblead.Purpose__c=Purpose;
        weblead.Address_r1__c=PlotNo;
        weblead.Address2_R__c=Street;
        weblead.Address_3_R__c=Locality;
        weblead.Pincode_R__c=PinCode;
        weblead.Occupation__c=Occupation;
        if(Occupation == 'Others')
        {
            weblead.Other_Occupation__c = others;
        }
            
        string str = '';
        for(string s: accomTypes)
        {
           str = str + ';' + s;  
        }

        weblead.Type_of_Accomodation__c = str;
               
        webLead.Company=CompanyName;
        if(CampaignName!=null)
        {
           webLead.Campaign_Name__c=CampaignName;
        }
        if(publisher!=null)
        {
            webLead.WebBanner_Source__c=publisher;
            
        }
        
        if(leadFirstName == '' )
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please Enter First Name');
           ApexPages.addMessage(myMsg);
           dateofvisit = dateofvisit;
           return null;
        }
        if (leadLastName == '')
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please Enter Last Name');
           ApexPages.addMessage(myMsg);
           return null;
        }
        if (DateOfVisit.length() == 0)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please Enter Date of Visit');
           ApexPages.addMessage(myMsg);
           return null;
        }
        if (IsValidPlotNo == false)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please Enter only Numeric character in Plot/Flat No.');
           ApexPages.addMessage(myMsg);
           return null;
        }
        if (city == 'None')
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please Enter City');
           ApexPages.addMessage(myMsg);
           return null;
        }
        if (IsValidPinCode == False)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please Enter Valid Pin Code');
           ApexPages.addMessage(myMsg);
           return null;
        }
         
        if (leadMobile == '' || IsValidMobile == False)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please Enter Valid Mobile');
           ApexPages.addMessage(myMsg);
           return null;
        }
        if (IsValidEmail == False)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please Enter Valid Email ');
           ApexPages.addMessage(myMsg);
           return null;
        }
        if (accomTypes.size() == 0)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please Select Accomodation Type ');
           ApexPages.addMessage(myMsg); 
           return null;
        }
        if (BudgetInLakhs == null || BudgetInLakhs == '' )
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please Enter Budget');
           ApexPages.addMessage(myMsg);
           return null;
        }

        /* Assign to project specific queues
        Map<string,string> ProjectQueueMap =  new Map<string,string>();
        Map<String, ProjectQueueMapping__c> pqMap = ProjectQueueMapping__c.getAll();
        List<String> pqNames = new List<String>();
        pqNames.addAll( pqMap.keySet());
        for (String p : pqNames) 
        {
            ProjectQueueMapping__c pqObj = pqMap.get(p);
            ProjectQueueMap.put(pqObj.Name,pqObj.Queue__c);
        }  
        
           if(projectId != null && projectId != '' && ProjectQueueMap.containsKey(projectId))  
           {               
                weblead.OwnerId = ProjectQueueMap.get(projectId);           
           }
         End Assign to project specific queues*/  
        
        insert(weblead); 
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
        //mail.setTemplateId('00XL0000000DYGF');
        mail.setTemplateId('00XD0000001rAxL');  
              
        mail.setTargetObjectId(weblead.id);
       // mail.setOrgWideEmailAddressId('0D2D000000000wo');        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

        system.debug('Come here End');
       }  
       catch(Exception e) { 
           ApexPages.addMessages(e);  
       
       return null;  
      }  
        PageReference p = Page.ThankYou;  
        p.getParameters().put('id',weblead.Id);
        p.setRedirect(true);  
        return p;  
     }

    public List<SelectOption> getItems() 
    {
        List<SelectOption> options = new List<SelectOption>();
        if(accTypeList.size()>0)
        {
            accTypeList.sort();
            for(String typeOfAcc : accTypeList)
            {
                options.add(new Selectoption(typeOfAcc ,typeOfAcc ));
            }
        }
        return options;
    }
    
    @isTest
    public static void test_Method()
    {
        Lead NewLead = new Lead();
       String page1 = '/apex/ExpressRegistrationPage?projectid='+[select Id,Name from Project_Name__c where Name = 'Lodha Fiorenza'].Id;
            PageReference pageRef1 = new PageReference(page1);        
            Test.setCurrentPage(pageRef1);        
        ExpressRegistrationExtension leadoper = new ExpressRegistrationExtension(new Apexpages.StandardController(NewLead));
        
        leadoper.DateOfVisit = '01/01/2011';
        leadoper.leadFirstName = '';        
        leadoper.saveLead();
                
        leadoper.leadFirstName = 'Test Data';
        leadoper.leadLastName = '';
        leadoper.saveLead();
        
        leadoper.leadFirstName = 'Test Data';
        leadoper.leadLastName = 'Test Data';
        leadoper.DateOfVisit = '';
        leadoper.saveLead();
        
        leadoper.leadFirstName = 'Test Data';
        leadoper.leadLastName = 'Test Data';
        leadoper.DateOfVisit = '01/01/2011';
        leadoper.IsValidPlotNo = false;
        leadoper.saveLead();
        
        leadoper.leadFirstName = 'Test Data';
        leadoper.leadLastName = 'Test Data';
        leadoper.DateOfVisit = '01/01/2011';
        leadoper.IsValidPlotNo = true;
        leadoper.city = 'None';
        leadoper.saveLead();
        
        leadoper.leadFirstName = 'Test Data';
        leadoper.leadLastName = 'Test Data';
        leadoper.DateOfVisit = '01/01/2011';
        leadoper.IsValidPlotNo = true;
        leadoper.city = '';
        leadoper.IsValidPinCode = false;
        leadoper.saveLead();
        
        leadoper.leadFirstName = 'Test Data';
        leadoper.leadLastName = 'Test Data';
        leadoper.DateOfVisit = '01/01/2011';
        leadoper.IsValidPlotNo = true;
        leadoper.city = '';
        leadoper.IsValidPinCode = true;
        leadoper.IsValidMobile = false;
        leadoper.saveLead();
        
        leadoper.leadFirstName = 'Test Data';
        leadoper.leadLastName = 'Test Data';
        leadoper.DateOfVisit = '01/01/2011';
        leadoper.IsValidPlotNo = true;
        leadoper.city = '';
        leadoper.IsValidPinCode = true;
        leadoper.IsValidMobile = true;
        leadoper.IsValidEmail = false;
        leadoper.saveLead();

        leadoper.SendErrorMail('Error');
        leadoper.accTypeList.add('3 BHK');
        leadoper.accTypeList.add('4 BHK');
        leadoper.getItems();
    }
}